
package com.zjff.myshop.admin.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zjff.myshop.admin.pojo.form.BatchIdsForm;
import com.zjff.myshop.admin.pojo.form.HomeInsertForm;
import com.zjff.myshop.admin.pojo.form.HomeUpdateForm;
import com.zjff.myshop.admin.pojo.vo.*;
import com.zjff.myshop.admin.service.HomeService;
import com.zjff.myshop.admin.service.OrderService;
import com.zjff.myshop.common.HomeTypeEnum;
import com.zjff.myshop.entity.Home;
import com.zjff.myshop.exception.BusinessException;
import com.zjff.myshop.util.BeanUtils;
import com.zjff.myshop.util.Result;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@RestController
@Api(value = "v1", tags = "84.后台管理系统-首页配置模块")
@RequestMapping("/admin/v1/home")
public class AdminHomeController {
    private static final Logger logger = LoggerFactory.getLogger(AdminHomeController.class);
    @Resource
    private HomeService homeService;
    @Resource
    private OrderService orderService;

    /**
     * 增加
     */
    @SaCheckRole("admin")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增首页配置项", notes = "新增首页配置项")
    @ApiImplicitParam(name = "form", value = "首页配置项", dataType = "HomeInsertForm", required = true)
    @PostMapping(value = "insert")
    public Result insert(@RequestBody @Valid @ApiParam(value = "首页配置项", required = true) HomeInsertForm form) {
        Home home = new Home();
        BeanUtils.copyProperties(form, home);
        if (homeService.insert(home)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 删除（单个）
     */
    @SaCheckRole("admin")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "删除单条记录", notes = "删除单个记录")
    @DeleteMapping(value = "/{id}")
    public Result deleteById(@PathVariable Integer id) {
        if (homeService.deleteById(id)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 删除批量
     */
    @SaCheckRole("admin")
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "批量删除首页配置项", notes = "批量删除首页配置项信息")
    @ApiImplicitParam(name = "form", value = "批量删除参数", dataType = "BatchIdsForm", required = true)
    @DeleteMapping(value = "/batch")
    public Result deleteByIds(@RequestBody @Valid BatchIdsForm form) {
        if (form == null || form.getIds().length < 1) {
            throw new BusinessException("数据不存在！");
        }
        if (homeService.deleteBatch(form.getIds())) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 更改
     */
    @SaCheckRole("admin")
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "更改首页配置项", notes = "更改首页配置项")
    @ApiImplicitParam(name = "form", value = "首页配置项", dataType = "HomeUpdateForm", required = true)
    @PostMapping(value = "/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "首页配置项", required = true) HomeUpdateForm form) {
        Home home = new Home();
        BeanUtils.copyProperties(form, home);
        if (homeService.update(home)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 详情
     */
    @SaCheckRole("admin")
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "查询单条详情", notes = "根据id查询")
    @GetMapping(value = "/{id}")
    public Result<HomeVO> info(@PathVariable(value = "id") Integer id) {
        Home home = homeService.selectById(id);
        if (home == null) {
            return Result.error("未查询到数据");
        }
        HomeVO homeVO = new HomeVO();
        BeanUtils.copyProperties(home, homeVO);
        return Result.ok(homeVO);
    }

    /**
     * 查询 首页轮播
     */
    @SaCheckRole("admin")
    @ApiOperationSupport(order = 31)
    @ApiOperation(value = "查询首页轮播配置", notes = "查询首页轮播配置")
    @GetMapping(value = "/carouselList")
    public Result<List<HomeCarouselVO>> carouselList() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("typeId", HomeTypeEnum.HOME_CAROUSEL.getId());
        return Result.ok(homeService.selectCarouselVOList(map));
    }

    /**
     * 查询 首页商品（按分类typeId)
     */
    @SaCheckRole("admin")
    @ApiOperationSupport(order = 32)
    @ApiOperation(value = "查询首页商品配置", notes = "查询首页商品配置")
    @ApiImplicitParam(name = "typeId", value = "首页配置类别（1轮播/2为你推荐/3新品上线/4热销商品）", dataType = "Integer", required = true)
    @GetMapping(value = "/goodsList")
    public Result<List<HomeGoodsVO>> goodsList(@RequestParam Integer typeId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("typeId", typeId);
        return Result.ok(homeService.selectGoodsVOList(map));
    }

    /**
     * 统计日期区间订单商品分类销售数据 GET
     */
    @ApiOperationSupport(order = 35)
    @ApiOperation(value = "统计日期区间订单商品分类销售数据", notes = "统计某段日期订单商品分类销售数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dtBgn", value = "开始日期（如:'20120101')", dataType = "String", required = true),
            @ApiImplicitParam(name = "dtEnd", value = "结束日期（如:'20121231')", dataType = "String", required = true),
    })
    @GetMapping(value = "/queryOrderSumByCategory")
    public Result<List<SumByCategoryVO>> queryOrderSumByCategory(@RequestParam String dtBgn, @RequestParam String dtEnd) {
        // 返回列表数据 :分类名categoryName、数量count、金额amount
        List<SumByCategoryVO> voList = orderService.queryOrderSumByCategory(dtBgn, dtEnd);
        return Result.ok(voList);
    }

    /**
     * 统计年度月份订单销售数据 GET
     */
    @ApiOperationSupport(order = 35)
    @ApiOperation(value = "商品分类销售统计", notes = "查询统计某段日期订单商品分类销售数据")
    @ApiImplicitParam(name = "year", value = "指定年份（如:2012)", dataType = "Integer", required = true)
    @GetMapping(value = "/queryOrderSumByMonth")
    public Result<List<SumByMonthVO>> queryOrderSumByMonth(@RequestParam Integer year) {
        // 返回列表数据 :月份month、数量count、金额amount
        List<SumByMonthVO> voList = orderService.queryOrderSumByMonth(year);
        return Result.ok(voList);
    }


}